import { createWebHashHistory, createRouter } from "vue-router";
import { ElMessage } from 'element-plus'; // 引入ElMessage

import index from '../view/index.vue'
import user from '../view/user.vue'
import product from "@/view/product.vue";
import role from '@/view/role.vue'
import brand from '@/view/brand.vue'
import category from "@/view/category.vue";
import userProduct from "@/view/userProduct.vue";
import login from '../view/Login.vue';
import hello from '../view/hello.vue';
import order from "@/view/order.vue";
import buy from "@/view/buy.vue";
import buycar from "@/view/buycar.vue";
import mycar from "@/view/mycar.vue";
import usercharts from "@/view/usercharts.vue";
import productcharts from "@/view/productcharts.vue";
import myorder from "@/view/myorder.vue";
import ordercharts from "@/view/ordercharts.vue";
import deepseek from "@/view/deepseek.vue";

const routes = [
    {
        path: '/',
        name: 'index',
        component: index,
        children: [
            {
                path: '/user',
                component: user,
                meta: { requiresAdmin: true }
            },
            {
                path: '/role',
                component: role,
                meta: { requiresAdmin: true }
            },
            {
                path: '/product',
                component: product,
                meta: { requiresAdmin: true }
            },
            {
                path: '/brand',
                component: brand,
                meta: { requiresAdmin: true }
            },
            {
                path: '/category',
                component: category,
                meta: { requiresAdmin: true }
            },
            {
                path: '/order',
                component: order,
                meta: { requiresAdmin: true }
            },
            {
                path: '/buycar',
                component: buycar,
                meta: { requiresAdmin: true }
            },
            {
                path: '/usercharts',
                component: usercharts,
                meta: { requiresAdmin: true }
            },
            {
                path: '/productcharts',
                component: productcharts,
                meta: { requiresAdmin: true }
            },
            {
                path: '/ordercharts',
                component: ordercharts,
                meta: { requiresAdmin: true }
            },
            {
                path: '/hello',
                component: hello
            },
            {
                path: '/buy',
                component: buy
            },
            {
                path: '/mycar',
                component: mycar
            },
            {
                path: '/myorder',
                component: myorder
            },
            {
                path: '/deepseek',
                component: deepseek
            }
        ]
    },
    // 补充登录页路由（之前可能遗漏了）
    {
        path: '/login',
        component: login
    }
]

// 先创建路由实例
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 再配置导航守卫
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAdmin) {
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
        if (userInfo.role_id === 1) {
            next();
        } else {
            ElMessage.error('您没有权限访问该页面，需要管理员权限');
            next('/hello');
        }
    } else {
        next();
    }
})

export default router;